import { GCtrl } from "../../lib/util/GCtrl";
import BtnVCodeSend from "../../lib/BtnVCodeSend";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(BtnVCodeSend)
    BtnVCodeSendCom = null;

    @property(cc.Node)
    pwSeeClose: cc.Node = null;

    @property(cc.Node)
    pwSureSeeClose: cc.Node = null;

    @property(cc.Node)
    lookNormal: cc.Node = null;

    @property(cc.Node)
    lookSureNormal: cc.Node = null;

    @property(cc.Label)
    lbPwCanNotSee: cc.Label = null;
    @property(cc.Label)
    lbPwSureCanNotSee: cc.Label = null;
    @property(cc.Sprite)
    titleSpr: cc.Sprite = null;

    @property(cc.EditBox)
    phoneEdit: cc.EditBox = null;
    @property(cc.EditBox)
    vfCodeEdit: cc.EditBox = null;


    @property(cc.EditBox)
    pwEdit: cc.EditBox = null;
    @property(cc.EditBox)
    pwVfEdit: cc.EditBox = null;

    private canSeePw = true;
    private canSeePwSure = true;

    // LIFE-CYCLE CALLBACKS:
    private myInfo = GCtrl.session.getPlayer();
    private isWithPhone: boolean = false;
    private titlePath = {
        withPhone: "HallDWC/DWC_BindPhone/image/CC_tb_yzsj",
        withoutPhone: "HallDWC/DWC_BindPhone/image/CC_tb_bdsj"
    };

    // onLoad () {}

    onEnable() {
        this.onShow();
    }
    onDisable() {
        this.onHide();
    }

    start() {

    }

    // update (dt) {}
    init(args) {

    }
    private updateTitle(isWithPhone: boolean) {
        isWithPhone ?
            GCtrl.loadSpriteRes(this.titlePath.withPhone, this.titleSpr) :
            GCtrl.loadSpriteRes(this.titlePath.withoutPhone, this.titleSpr);
    }
    private checkPhone() {
        let str = this.phoneEdit.string;
        if (!str) {
            GCtrl.GLUtils.showTips("请输入手机号");
            return false;
        }
        if (str.length != 11) {
            GCtrl.GLUtils.showTips("请输入正确的手机号");
            return false;
        }
        return true;
    }
    private checkVCode() {
        let str = this.vfCodeEdit.string;
        if (!str) {
            GCtrl.GLUtils.showTips("请输入验证码");
            return false;
        }
        return true;
    }
    private sendVerifyCode() {
        if (cc.vv.GLUtils.checkPhone(this.phoneEdit.string)) {
			var params = {
				cmd: "VerificationCode",
				msg: {
					phone: this.phoneEdit.string
				}
			};
			cc.vv.GLNet.netPost(params, this.BtnVCodeSendCom.sendSucessCallBack());
		} else {
			cc.vv.GLUtils.showTips("请输入正确的电话号码！");
		}


        // let cmd = "GetVcode";
        // let msg: any = {
        //     phone_number: this.phoneEdit.string
        // }
        // GCtrl.GLUtils.sendRequest(cmd, msg, (error, data) => {
        //     if (data.errcode)
        //         GCtrl.GLUtils.showTips(data.errcodedes);
        //     else {
        //         GCtrl.GLUtils.showTips("发送成功");
        //         this.BtnVCodeSendCom && this.BtnVCodeSendCom.sendSucessCallBack();
        //     }
        // });
    }
    private verifyOldPhoneReq() {
        let cmd = "PhoneCheck";
        let msg: any = {
            vcode: this.vfCodeEdit.string
        }
        GCtrl.GLUtils.sendRequest(cmd, msg, (error, data) => {
            if (data.errcodedes)
                GCtrl.GLUtils.showTips(data.errcodedes);
            else {
                this.isWithPhone = false;
                this.updateTitle(this.isWithPhone);
                this.phoneEdit.string = "";
                this.vfCodeEdit.string = "";
            }
        });
    }
    private bindPhoneReq() {
        //使用手机号注册
        cc.vv.GLNet.registerPhone_fpj(this.phoneEdit.string,this.vfCodeEdit.string)
    }
    onClickBack() {
        GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_BindPhone_path, GCtrl.UIMgr.closeType.destory);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickSendVerifyCode() {
        this.sendVerifyCode();
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickConfirm() {
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        if (!this.checkPhone()) return;
        if (!this.checkVCode()) return;
        //if(!this.checkISSavePw() )return;

        this.isWithPhone ? this.verifyOldPhoneReq() : this.bindPhoneReq();
    }
    onShow() {
        //this.phoneEdit.string = this.myInfo.phone + "";
        this.isWithPhone = !!this.myInfo.phone;
        this.updateTitle(this.isWithPhone);
    }
    onHide() {
        this.phoneEdit.string = "";
        this.vfCodeEdit.string = "";
    }

    checkISSavePw() {
        if (this.pwEdit.string == "") {
            GCtrl.GLUtils.showTips("输入正确的密码"); return false;
        }

        if (this.pwEdit.string.length < 8) {
            GCtrl.GLUtils.showTips("输入正确的密码"); return false;
        }

        if (this.pwEdit.string != this.pwVfEdit.string) {
            GCtrl.GLUtils.showTips("两次密码输入不一样"); return false;
        }
        return true;
    }

    getPwStringCanNotSee() {
        let str = this.pwEdit.string;
        let strCanNotSee = "";
        for (let i = 0; i < str.length; i++) {
            strCanNotSee += "*";
        }
        return strCanNotSee;
    }
    getPwSureStringCanNotSee() {
        let str = this.pwVfEdit.string;
        let strCanNotSee = "";
        for (let i = 0; i < str.length; i++) {
            strCanNotSee += "*";
        }
        return strCanNotSee;
    }

    onClickCanSeePw() {
        this.canSeePw = !this.canSeePw;
        this.setLookState();
    }

    onClickCanSeePwSure() {
        this.canSeePwSure = !this.canSeePwSure;
        this.setLookSureState();
    }

    setLookSureState() {
        if (!this.canSeePwSure) {
            this.pwSureSeeClose.active = true;
            this.lookSureNormal.active = false;
            this.pwVfEdit.textLabel.node.opacity = 0;
            this.pwVfEdit.textLabel.node.scale = 0;

            this.lbPwSureCanNotSee.node.active = true;
            this.lbPwSureCanNotSee.string = this.getPwSureStringCanNotSee();
        } else {
            this.pwVfEdit.textLabel.node.opacity = 255;
            this.pwVfEdit.textLabel.node.scale = 1;

            this.lbPwSureCanNotSee.node.active = false;
            this.pwSureSeeClose.active = false;
            this.lookSureNormal.active = true;
        }
    }
    setLookState() {
        if (!this.canSeePw) {
            this.pwSeeClose.active = true;
            this.lookNormal.active = false;
            this.pwEdit.textLabel.node.opacity = 0;
            this.pwEdit.textLabel.node.scale = 0;


            this.lbPwCanNotSee.node.active = true;
            this.lbPwCanNotSee.string = this.getPwStringCanNotSee();
        } else {
            this.pwEdit.textLabel.node.opacity = 255;
            this.pwEdit.textLabel.node.scale = 1;

            this.lbPwCanNotSee.node.active = false;
            this.pwSeeClose.active = false;
            this.lookNormal.active = true;
        }
    }

    pwTextChange() {
        // this.lbPwCanNotSee.string = this.getPwStringCanNotSee();
        this.setLookState();
    }
    pwSureTextChange() {
        //this.lbPwCanNotSee.string = this.getPwSureStringCanNotSee();
        this.setLookSureState();
    }
}
